package frame.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import frame.base.BootstrapTable;
import frame.base.IncrementIdService;
import frame.system.mapper.ConfConverterMapper;
import frame.system.qo.ConfQo;
import frame.system.to.ConfConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;


/**
 * ConfConverter 模块服务类
 * <br>
 * @author PeiWentao
 * @since 2020-09-24 11:23
 */
@Service
public class ConfConverterService extends IncrementIdService<ConfConverter, ConfQo> {
	@Autowired
    private ConfConverterMapper confConverterMapper;

    @Override
    protected Mapper<ConfConverter> mainMapper() {
        return confConverterMapper;
    }

    @Override
	public BootstrapTable<ConfConverter> pageList(final ConfQo qo) {
        //分页查询
        Page<ConfConverter> page = PageHelper.startPage(qo.page(), qo.size()).doSelectPage(() -> {
            // TODO 分页查询动态条件需要实现
            confConverterMapper.pageList(qo);
        });
        return new BootstrapTable<ConfConverter>(page.getTotal(),page.getResult());
	}

    @Override
    protected ConfConverter newInstanceT(Integer id) {
        return new ConfConverter(id);
    }

    /**
     *
     * @param convertor
     * @return
     */
    @Override
    public ConfConverter addSelective(ConfConverter convertor) {
        convertor.setId(null);
        int count = confConverterMapper.countTypeAndVal(convertor);
        if (count > 0) throw new IllegalArgumentException("该配置已存在，请检查！");
        return super.addSelective(convertor);
    }

    /**
     * 更新 不与原记录比较 查询是否配置有重复
     * @param convertor
     * @return
     */
    @Override
    public int updateSelective(ConfConverter convertor) {
        int count = confConverterMapper.countTypeAndVal(convertor);
        if (count > 0) throw new IllegalArgumentException("该配置已存在，请检查！");
        return super.updateSelective(convertor);
    }

    /**
     * 数据转换类型选择列表
     * @param qo .type 类型 必传
     * @return
     */
    public List<ConfConverter> selectList(final ConfQo qo) {
        return confConverterMapper.selectList(qo);
    }

    /**
     * 数据转换类型选择列表
     * @param type 类型 必传
     * @return
     */
    public List<ConfConverter> selectList(final String type) {
        return confConverterMapper.selectList(new ConfQo().setType(type));
    }
}
